package com.clstu.frame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

/**
 * 该示例用于演示 鼠标监听器的实现
 */
public class Mouse2 extends JFrame {
    JLabel jLabel = new JLabel();
    public static void main(String[] args) {
          new Mouse2();
    }
    public Mouse2(){
        super("鼠标事件");
        this.setBounds(10,10,400,300);
        this.setLayout(null);
        jLabel.setOpaque(true);
        jLabel.setBackground(Color.ORANGE);
        jLabel.setBounds(0,0,300,200);
        jLabel.addMouseListener(new MyMouseListener());//加入自定义的鼠标监听器
        this.add(jLabel);//标签放入窗口
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
class MyMouseListener implements MouseListener{

    @Override
    public void mouseClicked(MouseEvent e) {
        System.out.println("鼠标点击了 " + e.getClickCount()+" 次========");
        System.out.println("这次单击的坐标:  "+e.getX()+",  "+e.getY()+" ======");
    }

    @Override
    public void mousePressed(MouseEvent e) {
        switch (e.getButton()){
            case 1:
                System.out.println("鼠标被按下了===========左键");
                break;
            case 2:
                System.out.println("鼠标被按下了===========滚轮");
                break;
            case 3:
                System.out.println("鼠标被按下了===========右键");
                break;
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        System.out.println("鼠标释放了");
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        System.out.println("鼠标进入窗口了");
    }

    @Override
    public void mouseExited(MouseEvent e) {
        System.out.println("鼠标移出窗口了=======");
    }
}